package com.cqc.jdk8.Jutil.stream;

import com.cqc.eb.UserEB;
import org.junit.Before;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 去重
 */
public class DistinctTest {


    List<UserEB> userEBList = null;

    @Before
    public void before() {
        userEBList = getUserEBList();
    }


    @Test
    public void f1() {
        Stream.of("a", "a", "b", "c").distinct().forEach(System.out::println);
    }


    /**
     * 对象不能去重，因为是new的
     */
    @Test
    public void f2() {
        userEBList.stream()
                .distinct()
                .forEach(System.out::println);
    }

    /**
     * 根据对象属性去重：方法1：转成map,key相同的只保留一个
     */
    @Test
    public void f3() {
        Map<String, UserEB> map = userEBList.stream()
                .filter(x -> x.getAge() > 30)
                .collect(Collectors.toMap(UserEB::getName, userEB -> userEB, (k1, k2) -> k1));
        List<UserEB> list1 = new ArrayList<>(map.values());
    }

    /**
     * 根据对象属性去重
     */
    @Test
    public void f4() {
        //方法2
        ArrayList<UserEB> list1 = userEBList.stream()
                .filter(x -> null != x && x.getAge() > 30)
                .collect(
                        Collectors.collectingAndThen(Collectors.toMap(UserEB::getName, o -> o, (k1, k2) -> k1), x -> new ArrayList<>(x.values()))
                );
        // 方法3
        Map<String, UserEB> map2 = userEBList.stream().filter(o -> o != null && o.getName() != null)
                .collect(HashMap::new, (m, o) -> m.put(o.getName(), o), HashMap::putAll);
        List<UserEB> list2 = new ArrayList<>(map2.values());

        //BiConsumer有2个 参数，没有返回值。
        //HashMap<String, UserEB> collect = userEBList.stream().filter(o -> o != null && o.getName() != null)
        //        .collect(HashMap::new, (m, o) -> m.put(o.getName(), o), new BiConsumer<HashMap<String, UserEB>, HashMap<String, UserEB>>() {
        //            @Override
        //            public void accept(HashMap<String, UserEB> hashMap1, HashMap<String, UserEB> hashMap2) {
        //                hashMap1.putAll(hashMap2);
        //            }
        //        });
        // 方法4
        List<UserEB> list4 = userEBList.stream()
                .filter(o -> o != null && o.getName() != null)
                .collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(UserEB::getName)))
                                , ArrayList<UserEB>::new)
                );

        System.out.println(1);

    }


    private List<UserEB> getUserEBList() {
        userEBList = new ArrayList<>();
        userEBList.add(new UserEB("刘诗诗", 33));
        userEBList.add(new UserEB("刘诗诗", 33));
        userEBList.add(new UserEB("刘诗诗", 31));
        userEBList.add(new UserEB("刘亦菲", 33));
        userEBList.add(new UserEB("古力娜扎", 30));
        //userEBList.add(new UserEB("陈都灵", 29));
        //userEBList.add(new UserEB("杨紫", 28));
        //userEBList.add(new UserEB("林青霞", 60));
        return userEBList;
    }

}
